package com.mall.common.domain.component;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.mall.common.domain.BaseEntity;


/**
 * 插件
 */
@Entity
@Table(name = "s_component")
@JsonIgnoreProperties(ignoreUnknown=true) 
public class ComponentDomain extends BaseEntity {
	
	private static final long serialVersionUID = -2518763738616308312L;
	private String name;//名称
	private String version;//插件版本
	private String shopVersion;//商城版本
	private String author;//作者
	private Boolean isOfficial; //是否正式环境
	private String description;//备注
	private String installState;//安装状态 (1: 安装， 2: 没安装)
	private String enableState;//开启状态  (1: 开启， 2: 没开启)
	private String errormessage;//错误信息
	private String componentId;//插件ID
	@JsonIgnore private ComponentBundleDomain componentBundle;//插件桩
	private String config;//参数配置
	@JsonIgnore private PaymentTypeDomain paymemtType;//支付方式
	private Integer orders;//排序
	
	@Transient
	public JSONObject getConfigJson(String key) {
		if(StringUtils.isEmpty(config)){
			return null;
		}
		if(StringUtils.isEmpty(key)){
			return JSONObject.fromObject(this.getConfig());
		}
		return JSONObject.fromObject(this.getConfig()).getJSONObject(key);
	}
	
	@JsonSetter
	public void setConfig(JSONObject config){
		this.config = config.toString();
	}
	
	@Column(unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getShopVersion() {
		return shopVersion;
	}

	public void setShopVersion(String shopVersion) {
		this.shopVersion = shopVersion;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getInstallState() {
		return installState;
	}

	public void setInstallState(String installState) {
		this.installState = installState;
	}

	public String getEnableState() {
		return enableState;
	}

	public void setEnableState(String enableState) {
		this.enableState = enableState;
	}

	public String getErrormessage() {
		return errormessage;
	}

	public void setErrormessage(String errormessage) {
		this.errormessage = errormessage;
	}

	@Column(unique = true)
	public String getComponentId() {
		return componentId;
	}

	public void setComponentId(String componentId) {
		this.componentId = componentId;
	}

	@Column(length=2000)
	public String getConfig() {
		return config;
	}

	public void setConfig(String config) {
		this.config = config;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public ComponentBundleDomain getComponentBundle() {
		return componentBundle;
	}

	public void setComponentBundle(ComponentBundleDomain componentBundle) {
		this.componentBundle = componentBundle;
	}

	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "paymentType_id")
	@Cascade(value = { CascadeType.DELETE })
	public PaymentTypeDomain getPaymemtType() {
		return paymemtType;
	}

	public void setPaymemtType(PaymentTypeDomain paymemtType) {
		this.paymemtType = paymemtType;
	}

	public Boolean getIsOfficial() {
		return isOfficial;
	}

	public void setIsOfficial(Boolean isOfficial) {
		this.isOfficial = isOfficial;
	}

	public Integer getOrders() {
		return orders;
	}

	public void setOrders(Integer orders) {
		this.orders = orders;
	}

}
